home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / curses / textpad.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  7KB  |  220 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Simple textbox editing widget with Emacs-like keybindings.'''
  5. import curses
  6. import curses.ascii as curses
  7.  
  8. def rectangle(win, uly, ulx, lry, lrx):
  9.     '''Draw a rectangle with corners at the provided upper-left
  10.     and lower-right coordinates.
  11.     '''
  12.     win.vline(uly + 1, ulx, curses.ACS_VLINE, lry - uly - 1)
  13.     win.hline(uly, ulx + 1, curses.ACS_HLINE, lrx - ulx - 1)
  14.     win.hline(lry, ulx + 1, curses.ACS_HLINE, lrx - ulx - 1)
  15.     win.vline(uly + 1, lrx, curses.ACS_VLINE, lry - uly - 1)
  16.     win.addch(uly, ulx, curses.ACS_ULCORNER)
  17.     win.addch(uly, lrx, curses.ACS_URCORNER)
  18.     win.addch(lry, lrx, curses.ACS_LRCORNER)
  19.     win.addch(lry, ulx, curses.ACS_LLCORNER)
  20.  
  21.  
  22. class Textbox:
  23.     '''Editing widget using the interior of a window object.
  24.      Supports the following Emacs-like key bindings:
  25.  
  26.     Ctrl-A      Go to left edge of window.
  27.     Ctrl-B      Cursor left, wrapping to previous line if appropriate.
  28.     Ctrl-D      Delete character under cursor.
  29.     Ctrl-E      Go to right edge (stripspaces off) or end of line (stripspaces on).
  30.     Ctrl-F      Cursor right, wrapping to next line when appropriate.
  31.     Ctrl-G      Terminate, returning the window contents.
  32.     Ctrl-H      Delete character backward.
  33.     Ctrl-J      Terminate if the window is 1 line, otherwise insert newline.
  34.     Ctrl-K      If line is blank, delete it, otherwise clear to end of line.
  35.     Ctrl-L      Refresh screen.
  36.     Ctrl-N      Cursor down; move down one line.
  37.     Ctrl-O      Insert a blank line at cursor location.
  38.     Ctrl-P      Cursor up; move up one line.
  39.  
  40.     Move operations do nothing if the cursor is at an edge where the movement
  41.     is not possible.  The following synonyms are supported where possible:
  42.  
  43.     KEY_LEFT = Ctrl-B, KEY_RIGHT = Ctrl-F, KEY_UP = Ctrl-P, KEY_DOWN = Ctrl-N
  44.     KEY_BACKSPACE = Ctrl-h
  45.     '''
  46.     
  47.     def __init__(self, win, insert_mode = False):
  48.         self.win = win
  49.         self.insert_mode = insert_mode
  50.         (self.maxy, self.maxx) = win.getmaxyx()
  51.         self.maxy = self.maxy - 1
  52.         self.maxx = self.maxx - 1
  53.         self.stripspaces = 1
  54.         self.lastcmd = None
  55.         win.keypad(1)
  56.  
  57.     
  58.     def _end_of_line(self, y):
  59.         '''Go to the location of the first blank on the given line,
  60.         returning the index of the last non-blank character.'''
  61.         last = self.maxx
  62.         while True:
  63.             if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP:
  64.                 last = min(self.maxx, last + 1)
  65.                 break
  66.             elif last == 0:
  67.                 break
  68.             
  69.             last = last - 1
  70.         return last
  71.  
  72.     
  73.     def _insert_printable_char(self, ch):
  74.         (y, x) = self.win.getyx()
  75.         if y < self.maxy or x < self.maxx:
  76.             if self.insert_mode:
  77.                 oldch = self.win.inch()
  78.             
  79.             
  80.             try:
  81.                 self.win.addch(ch)
  82.             except curses.error:
  83.                 pass
  84.  
  85.             if self.insert_mode:
  86.                 (backy, backx) = self.win.getyx()
  87.                 if curses.ascii.isprint(oldch):
  88.                     self._insert_printable_char(oldch)
  89.                     self.win.move(backy, backx)
  90.                 
  91.             
  92.         
  93.  
  94.     
  95.     def do_command(self, ch):
  96.         '''Process a single editing command.'''
  97.         (y, x) = self.win.getyx()
  98.         self.lastcmd = ch
  99.         if curses.ascii.isprint(ch):
  100.             if y < self.maxy or x < self.maxx:
  101.                 self._insert_printable_char(ch)
  102.             
  103.         elif ch == curses.ascii.SOH:
  104.             self.win.move(y, 0)
  105.         elif ch in (curses.ascii.STX, curses.KEY_LEFT, curses.ascii.BS, curses.KEY_BACKSPACE):
  106.             if x > 0:
  107.                 self.win.move(y, x - 1)
  108.             elif y == 0:
  109.                 pass
  110.             elif self.stripspaces:
  111.                 self.win.move(y - 1, self._end_of_line(y - 1))
  112.             else:
  113.                 self.win.move(y - 1, self.maxx)
  114.             if ch in (curses.ascii.BS, curses.KEY_BACKSPACE):
  115.                 self.win.delch()
  116.             
  117.         elif ch == curses.ascii.EOT:
  118.             self.win.delch()
  119.         elif ch == curses.ascii.ENQ:
  120.             if self.stripspaces:
  121.                 self.win.move(y, self._end_of_line(y))
  122.             else:
  123.                 self.win.move(y, self.maxx)
  124.         elif ch in (curses.ascii.ACK, curses.KEY_RIGHT):
  125.             if x < self.maxx:
  126.                 self.win.move(y, x + 1)
  127.             elif y == self.maxy:
  128.                 pass
  129.             else:
  130.                 self.win.move(y + 1, 0)
  131.         elif ch == curses.ascii.BEL:
  132.             return 0
  133.         if ch == curses.ascii.NL:
  134.             if self.maxy == 0:
  135.                 return 0
  136.             if y < self.maxy:
  137.                 self.win.move(y + 1, 0)
  138.             
  139.         elif ch == curses.ascii.VT:
  140.             if x == 0 and self._end_of_line(y) == 0:
  141.                 self.win.deleteln()
  142.             else:
  143.                 self.win.move(y, x)
  144.                 self.win.clrtoeol()
  145.         elif ch == curses.ascii.FF:
  146.             self.win.refresh()
  147.         elif ch in (curses.ascii.SO, curses.KEY_DOWN):
  148.             if y < self.maxy:
  149.                 self.win.move(y + 1, x)
  150.                 if x > self._end_of_line(y + 1):
  151.                     self.win.move(y + 1, self._end_of_line(y + 1))
  152.                 
  153.             
  154.         elif ch == curses.ascii.SI:
  155.             self.win.insertln()
  156.         elif ch in (curses.ascii.DLE, curses.KEY_UP):
  157.             if y > 0:
  158.                 self.win.move(y - 1, x)
  159.                 if x > self._end_of_line(y - 1):
  160.                     self.win.move(y - 1, self._end_of_line(y - 1))
  161.                 
  162.             
  163.         
  164.         return 1
  165.  
  166.     
  167.     def gather(self):
  168.         '''Collect and return the contents of the window.'''
  169.         result = ''
  170.         for y in range(self.maxy + 1):
  171.             self.win.move(y, 0)
  172.             stop = self._end_of_line(y)
  173.             if stop == 0 and self.stripspaces:
  174.                 continue
  175.             
  176.             for x in range(self.maxx + 1):
  177.                 if self.stripspaces and x > stop:
  178.                     break
  179.                 
  180.                 result = result + chr(curses.ascii.ascii(self.win.inch(y, x)))
  181.             
  182.             if self.maxy > 0:
  183.                 result = result + '\n'
  184.                 continue
  185.         
  186.         return result
  187.  
  188.     
  189.     def edit(self, validate = None):
  190.         '''Edit in the widget window and collect the results.'''
  191.         while None:
  192.             ch = self.win.getch()
  193.             if validate:
  194.                 ch = validate(ch)
  195.             
  196.             if not ch:
  197.                 continue
  198.             
  199.             if not self.do_command(ch):
  200.                 break
  201.             
  202.             continue
  203.             return self.gather()
  204.  
  205.  
  206. if __name__ == '__main__':
  207.     
  208.     def test_editbox(stdscr):
  209.         (ncols, nlines) = (9, 4)
  210.         (uly, ulx) = (15, 20)
  211.         stdscr.addstr(uly - 2, ulx, 'Use Ctrl-G to end editing.')
  212.         win = curses.newwin(nlines, ncols, uly, ulx)
  213.         rectangle(stdscr, uly - 1, ulx - 1, uly + nlines, ulx + ncols)
  214.         stdscr.refresh()
  215.         return Textbox(win).edit()
  216.  
  217.     str = curses.wrapper(test_editbox)
  218.     print 'Contents of text box:', repr(str)
  219.  
  220.